package com.wang.see.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.wang.see.pojo.Image;

import lombok.Getter;

/**
 * 查询图片返回List
 * 
 * @author WangHao
 * @Date 2017年11月3日 14:45:03
 */
public class FindImgsUtil {

	private static Logger logger = Logger.getLogger(FindImgsUtil.class);
	
	@Getter
	List<Image> imgs = new ArrayList<>();
	
	public FindImgsUtil(String path) {
		long date1 = new Date().getTime();
		FindImgs(path);
		long date2 = new Date().getTime();
		logger.info("本次共找到"+imgs.size()+"张图片文件,耗时"+(date2-date1)+"秒,数据为："+imgs);
	}
	
	public void FindImgs(String path) {

		File file = new File(path);
		if (!file.exists()) {
			logger.warn("文件夹不存在!");
			return;
		}

		File[] files = file.listFiles();
		if (files.length == 0) {
			logger.warn("文件夹是空的!");
			return;
		}

		for (File file2 : files) {
			if (file2.isDirectory()) {
				logger.info("即将进入文件夹<" + file2.getAbsolutePath() + ">查看文件");
				FindImgs(file2.getAbsolutePath().replace("\\", "/"));
				continue;
			}

			String abspath = file2.getAbsolutePath().toLowerCase();
			if (!(abspath.endsWith("jpg") || abspath.endsWith("png") || abspath.endsWith("jpeg")
					|| abspath.endsWith("gif"))) {
				continue;
			}
			
			logger.info("查询到图片文件：" + file2.getName());
			String imgFolderName = file2.getAbsolutePath().replace("\\", "/");
			if (imgFolderName.split("/").length < 2)
				imgFolderName = "other";
			
			imgFolderName = imgFolderName.split("/")[(imgFolderName.split("/").length - 2)];
			imgs.add(new Image(file2.getName(), file2.getAbsolutePath(), imgFolderName));
		}

	}

	public static void main(String[] args) {
//		FindImgs("C:/Users/bs/Pictures");
//		System.out.println(imgs.size()+"######"+imgs);
	}
}
